package com.epalmpay.service.mall.impl;

import java.util.*;

import com.epalmpay.dto.mall.*;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.enumdef.CommonEnum;
import com.epalmpay.mapper.MallCartMapper;
import com.epalmpay.mapper.MallEntityGoodMapper;
import com.epalmpay.mapper.MallGoodMapper;
import com.epalmpay.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.entity.MallCategory;
import com.epalmpay.mapper.MallCategoryMapper;
import com.epalmpay.service.mall.IMallCategoryService;
import com.epalmpay.util.PropertyCopyUtil;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Created by lifayi on 2017/5/16.
 */
@Service
public class MallCategoryServiceImpl implements IMallCategoryService {

    @Resource
    private MallCategoryMapper mallCategoryMapper;
    @Resource
    private MallGoodMapper mallGoodMapper;
    @Resource
    private MallEntityGoodMapper mallEntityGoodMapper;

    public List<MallCategoryOutDTO> mallCategory(MallCategoryQueryDTO mallCategoryQueryDTO) throws Exception {
        Long groupId = mallCategoryQueryDTO.getGroupId();
        Byte goodBigtype = mallCategoryQueryDTO.getGoodBigtype();

        List<MallCategoryOutDTO> mallCategoryOutDTOList = new ArrayList<MallCategoryOutDTO>();

        List<MallCategory> oneLevelCategory = mallCategoryMapper.listLevelCategory(groupId, null, goodBigtype);

        for(int i = 0; i < oneLevelCategory.size(); i++) {
            MallCategoryOutDTO mallCategoryOutDTO = new MallCategoryOutDTO();
            PropertyCopyUtil.copyProperties(oneLevelCategory.get(i), mallCategoryOutDTO);

            //获取本类目下商品数量
            Map<String, Long> info = mallGoodMapper.getGoodNumberByOneCategoryId(oneLevelCategory.get(i).getId(), goodBigtype);
            mallCategoryOutDTO.setGoodsNumber(info.get("goodNumbers"));

            List<MallCategory> twoLevelCategory = mallCategoryMapper.listLevelCategory(groupId, oneLevelCategory.get(i).getId(), goodBigtype);
            List<MallCategoryOutDTO> mallCategoryOutDTOList1 = new ArrayList<MallCategoryOutDTO>();
            for(int j = 0; j < twoLevelCategory.size(); j++) {
                MallCategoryOutDTO mallCategoryOutDTO1 = new MallCategoryOutDTO();
                PropertyCopyUtil.copyProperties(twoLevelCategory.get(j), mallCategoryOutDTO1);

                //获取子类目下商品数量
                Map<String, Long> info1 = mallGoodMapper.getGoodNumberByTwoCategoryId(twoLevelCategory.get(j).getId(),goodBigtype);
                mallCategoryOutDTO1.setGoodsNumber(info1.get("goodNumbers"));

                mallCategoryOutDTOList1.add(mallCategoryOutDTO1);
            }
            mallCategoryOutDTO.setMallCategoryOutDTOList(mallCategoryOutDTOList1);
            mallCategoryOutDTOList.add(mallCategoryOutDTO);
        }

        return mallCategoryOutDTOList;
    }

    public MallCategoryOutDTO mallCategoryAdd(MallCategoryInDTO mallCategoryInDTO) throws Exception {
        Date now = new Date();
        MallCategory mallCategory = new MallCategory();
        PropertyCopyUtil.copyProperties(mallCategoryInDTO, mallCategory);
        mallCategory.setGroupId(mallCategoryInDTO.getGroupId());
        mallCategory.setGmtCreate(now);
        mallCategoryMapper.insert(mallCategory);

        MallCategoryOutDTO mallCategoryOutDTO = new MallCategoryOutDTO();
        PropertyCopyUtil.copyProperties(mallCategoryInDTO, mallCategoryOutDTO);
        mallCategoryOutDTO.setGmtCreate(now);
        mallCategoryOutDTO.setId(mallCategory.getId());

        return mallCategoryOutDTO;
    }

    @Override
    public MallCategoryOutDTO selectAndCheckCategoryName(MallCategoryInDTO mallCategoryInDTO) throws Exception {
        MallCategory mallCategory = new MallCategory();
        MallCategoryOutDTO mallCategoryOutDTO = new MallCategoryOutDTO();
        PropertyCopyUtil.copyProperties(mallCategoryInDTO,mallCategory);
        MallCategory resultMallCategory=mallCategoryMapper.selectAndCheckCategoryName(mallCategory);
        if(resultMallCategory!=null){
            //证明重名
            mallCategoryOutDTO.setErrorCode("-1","重名");
        }else {
            mallCategoryOutDTO.setErrorCode("1","未重名");
        }
        return mallCategoryOutDTO;
    }

    @Override
    public int updateCategoryById(MallCategoryInDTO mallCategoryInDTO) throws Exception {
        MallCategory mallCategory = new MallCategory();
        mallCategoryInDTO.setGmtCreate(new Date());
        PropertyCopyUtil.copyProperties(mallCategoryInDTO,mallCategory);
        return mallCategoryMapper.updateByPrimaryKeySelective(mallCategory);

    }

    public BaseOutDTO mallCategoryResort(List<MallCategoryInDTO> mallCategoryInDTOList) throws Exception {
        BaseOutDTO baseOutDTO = new BaseOutDTO();

        for(int i = 0; i < mallCategoryInDTOList.size(); i++) {
            MallCategory mallCategory = new MallCategory();
            PropertyCopyUtil.copyProperties(mallCategoryInDTOList.get(i), mallCategory);
            mallCategory.setGroupId(mallCategoryInDTOList.get(i).getGroupId());
            mallCategoryMapper.updateByPrimaryKeySelective(mallCategory);
        }
        baseOutDTO.setErrorCode(Rescode.SUCCESS);
        return baseOutDTO;
    }

    @Override
    public BaseOutDTO mallCategoryDel(MallCategoryInDTO mallCategoryInDTO) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        Long id = mallCategoryInDTO.getId();
        Long groupId = mallCategoryInDTO.getGroupId();

        MallCategory mallCategory = mallCategoryMapper.selectByPrimaryKey(id);
        if(mallCategory.getParentId() == null) {
            Map<String, Long> info = mallCategoryMapper.listChildCategoryByParentId(id);
            if(info.get("amount") > 0) {
                baseOutDTO.setErrorCode(Rescode.FAIL);
                return baseOutDTO;
            } else {
                mallCategoryMapper.deleteByPrimaryKeyAndGroupId(id, groupId);
            }
        } else {
            delChildCategory(id, groupId);
        }

        baseOutDTO.setErrorCode(Rescode.SUCCESS);
        return baseOutDTO;
    }

    @Override
    public BaseOutDTO mallCategoryIcon(MallCategoryInDTO mallCategoryInDTO) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        MallCategory mallCategory = new MallCategory();
        mallCategory.setId(mallCategoryInDTO.getId());
        mallCategory.setGroupId(mallCategoryInDTO.getGroupId());
        mallCategory.setIcon(mallCategoryInDTO.getIcon());
        int i = mallCategoryMapper.updateByPrimaryKeySelective(mallCategory);
        if(i != 1) {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        } else {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        }
        return baseOutDTO;
    }

    @Override
    public CategoryOutDTO getCategory(MallCategoryQueryDTO mallCategoryQueryDTO) {
        CategoryOutDTO categoryOutDTO = new CategoryOutDTO();
        List<CategoryDTO> categoryDTOList = new ArrayList<>();
        List<CategoryDTO> categoryDTOList1 = new ArrayList<>();

        List<MallCategory> mallCategoryList = mallCategoryMapper.listLevelCategory(mallCategoryQueryDTO.getGroupId(), null, mallCategoryQueryDTO.getGoodBigtype());
        for(int i = 0; i < mallCategoryList.size(); i++) {
            MallCategory mallCategory = mallCategoryList.get(i);
            CategoryDTO categoryDTO = new CategoryDTO();
            categoryDTO.setId(mallCategory.getId());
            categoryDTO.setName(mallCategory.getName());
            categoryDTOList.add(categoryDTO);
        }

        categoryOutDTO.setCategoryFirstLevelList(categoryDTOList);

        Long id = mallCategoryQueryDTO.getParentId();
        if(id == null && categoryDTOList.size() > 0) {
            id = mallCategoryList.get(0).getId();
        }

        List<MallCategory> mallCategoryList1 = mallCategoryMapper.listLevelCategory(mallCategoryQueryDTO.getGroupId(), id, mallCategoryQueryDTO.getGoodBigtype());
        for(int j = 0; j < mallCategoryList1.size(); j++) {
            MallCategory mallCategory = mallCategoryList1.get(j);
            CategoryDTO categoryDTO = new CategoryDTO();
            categoryDTO.setId(mallCategory.getId());
            categoryDTO.setName(mallCategory.getName());
            categoryDTOList1.add(categoryDTO);
        }
        categoryOutDTO.setCategorySecondLevelList(categoryDTOList1);

        return categoryOutDTO;
    }

    @Transactional
    void delChildCategory(Long id, Long groupId) {
        int i = mallCategoryMapper.deleteByPrimaryKeyAndGroupId(id, groupId);
        if(i == 1) {
            mallGoodMapper.deleteCategory(id);
        }
    }

    @Override
    public List<MallCategoryOutDTO> getMallCategoryNameAndID(MallCategoryQueryDTO mallCategoryQueryDTO) throws Exception {
        List<MallCategory> mallCategoryList = mallCategoryMapper.getMallCategoryNameAndID(mallCategoryQueryDTO);
        List<MallCategory> mallCategoryList1 = null;

        //如果是获取一级目录，则同时也把排序为1的一级目录的子目录也查询出来
        if(mallCategoryList.size() > 0) {
            if(mallCategoryQueryDTO.getParentId() == null) {
                mallCategoryQueryDTO.setParentId(mallCategoryList.get(0).getId());
                mallCategoryList1 = mallCategoryMapper.getMallCategoryNameAndID(mallCategoryQueryDTO);
            }
        }

        List<MallCategoryOutDTO> mallCategoryOutDTOList = new ArrayList<MallCategoryOutDTO>();
        for(int i = 0; i < mallCategoryList.size(); i++) {
            MallCategoryOutDTO mallCategoryOutDTO = new MallCategoryOutDTO();
            PropertyCopyUtil.copyProperties(mallCategoryList.get(i), mallCategoryOutDTO);
            if(i == 0 && mallCategoryList1 != null) {
                List<MallCategoryOutDTO> mallCategoryOutDTOList1 = new ArrayList<MallCategoryOutDTO>();
                for(int j = 0; j < mallCategoryList1.size(); j++) {
                    MallCategoryOutDTO mallCategoryOutDTO1 = new MallCategoryOutDTO();
                    PropertyCopyUtil.copyProperties(mallCategoryList1.get(j), mallCategoryOutDTO1);
                    mallCategoryOutDTOList1.add(mallCategoryOutDTO1);
                }
                mallCategoryOutDTO.setMallCategoryOutDTOList(mallCategoryOutDTOList1);
            }
            if(mallCategoryQueryDTO.getParentId() != null) {
                Long goodNumber = mallGoodMapper.getGoodNumberByTwoCategoryId(mallCategoryOutDTO.getId(), (byte) CommonEnum.GoodType.Service.getType()).get("goodNumbers");
                mallCategoryOutDTO.setGoodsNumber(goodNumber);
            }

            mallCategoryOutDTOList.add(mallCategoryOutDTO);
        }
        return mallCategoryOutDTOList;
    }

    @Override
    public List<MallCategory> getMallCategoryNameAndID2(MallCategoryQueryDTO mallCategoryQueryDTO) throws Exception {
        List<MallCategory> mallCategoryList = mallCategoryMapper.getMallCategoryNameAndID(mallCategoryQueryDTO);


        //如果是获取一级目录，则同时也把排序为1的一级目录的子目录也查询出来
        if(mallCategoryList.size() > 0) {
            for(MallCategory mallCategory : mallCategoryList) {
                mallCategoryQueryDTO.setParentId(mallCategory.getId());
                List<MallCategory> mallCategoryList1 = mallCategoryMapper.getMallCategoryNameAndID(mallCategoryQueryDTO);
                mallCategory.setMallCategoryList(mallCategoryList1);
            }
        }

        return mallCategoryList;
    }

    @Override
    public List<MallCategoryOutDTO> getShareMallCategoryNameAndID(MallCategoryQueryDTO mallCategoryQueryDTO) throws Exception {
        Long groupId = mallCategoryQueryDTO.getGroupId();
//
//        List<MallCategory> oneLevelCategory = mallCategoryMapper.listLevelCategory(groupId, null, goodBigtype);

        List<MallCategory> mallCategoryList = mallCategoryMapper.getShareMallCategoryNameAndID(mallCategoryQueryDTO);
        List<MallCategory> mallCategoryList1 = null;

        //如果是获取一级目录，则同时也把排序为1的一级目录的子目录也查询出来
        if(mallCategoryList.size() > 0) {
            if(mallCategoryQueryDTO.getParentId() == null) {
                mallCategoryQueryDTO.setParentId(mallCategoryList.get(0).getId());
                mallCategoryList1 = mallCategoryMapper.getShareMallCategoryNameAndID(mallCategoryQueryDTO);
            }
        }

        List<MallCategoryOutDTO> mallCategoryOutDTOList = new ArrayList<MallCategoryOutDTO>();
        for(int i = 0; i < mallCategoryList.size(); i++) {
            MallCategoryOutDTO mallCategoryOutDTO = new MallCategoryOutDTO();
            PropertyCopyUtil.copyProperties(mallCategoryList.get(i), mallCategoryOutDTO);

            //获取本类目下商品数量
            Map<String, Long> info = mallGoodMapper.getGoodNumberByOneCategoryId(mallCategoryList.get(i).getId(), (byte) mallCategoryList.get(i).getType().longValue());
            mallCategoryOutDTO.setGoodsNumber(info.get("goodNumbers"));

            List<MallCategory> twoLevelCategory = mallCategoryMapper.listLevelCategory(groupId, mallCategoryList.get(i).getId(),(byte) mallCategoryList.get(i).getType().longValue());

//            if(i == 0 && mallCategoryList1 != null) {
//                List<MallCategoryOutDTO> mallCategoryOutDTOList1 = new ArrayList<MallCategoryOutDTO>();
//                for(int j = 0; j < mallCategoryList1.size(); j++) {
//                    MallCategoryOutDTO mallCategoryOutDTO1 = new MallCategoryOutDTO();
//                    PropertyCopyUtil.copyProperties(mallCategoryList1.get(j), mallCategoryOutDTO1);
//                    mallCategoryOutDTOList1.add(mallCategoryOutDTO1);
//                }
//                mallCategoryOutDTO.setMallCategoryOutDTOList(mallCategoryOutDTOList1);
//            }
//            mallCategoryOutDTOList.add(mallCategoryOutDTO);
//            if(i == 0 && mallCategoryList1 != null) {
                List<MallCategoryOutDTO> mallCategoryOutDTOList1 = new ArrayList<MallCategoryOutDTO>();
                for (int j = 0; j < twoLevelCategory.size(); j++) {
                    MallCategoryOutDTO mallCategoryOutDTO1 = new MallCategoryOutDTO();
                    PropertyCopyUtil.copyProperties(twoLevelCategory.get(j), mallCategoryOutDTO1);

                    //获取子类目下商品数量
                    Map<String, Long> info1 = mallGoodMapper.getGoodNumberByTwoCategoryId(twoLevelCategory.get(j).getId(), (byte) mallCategoryList.get(i).getType().longValue());
                    mallCategoryOutDTO1.setGoodsNumber(info1.get("goodNumbers"));

                    mallCategoryOutDTOList1.add(mallCategoryOutDTO1);
                }
                mallCategoryOutDTO.setMallCategoryOutDTOList(mallCategoryOutDTOList1);
                 mallCategoryOutDTOList.add(mallCategoryOutDTO);
//            }

        }
        return mallCategoryOutDTOList;
    }




}
